package com.nanhai.buddhist.college.module.study.service.studyplan;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.module.study.controller.admin.studyplan.vo.StudyPlanTeacherCreateReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.studyplan.vo.StudyPlanTeacherExportReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.studyplan.vo.StudyPlanTeacherPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.studyplan.vo.StudyPlanTeacherUpdateReqVO;
import com.nanhai.buddhist.college.module.study.convert.studyplan.StudyPlanTeacherConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyplan.StudyPlanSubjectDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyplan.StudyPlanTeacherDO;
import com.nanhai.buddhist.college.module.study.dal.mysql.studyplan.StudyPlanSubjectMapper;
import com.nanhai.buddhist.college.module.study.dal.mysql.studyplan.StudyPlanTeacherMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.PLAN_TEACHER_NOT_EXISTS;

/**
 * 教学计划教师信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StudyPlanTeacherServiceImpl implements StudyPlanTeacherService {

    @Resource
    private StudyPlanTeacherMapper planTeacherMapper;

    @Resource
    private StudyPlanSubjectMapper studyPlanSubjectMapper;

    @Override
    public Long createPlanTeacher(StudyPlanTeacherCreateReqVO createReqVO) {
        // 插入
        StudyPlanTeacherDO planTeacher = StudyPlanTeacherConvert.INSTANCE.convert(createReqVO);
        planTeacherMapper.insert(planTeacher);
        // 返回
        return planTeacher.getId();
    }

    @Override
    public void updatePlanTeacher(StudyPlanTeacherUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePlanTeacherExists(updateReqVO.getId());
        // 更新
        StudyPlanTeacherDO updateObj = StudyPlanTeacherConvert.INSTANCE.convert(updateReqVO);
        planTeacherMapper.updateById(updateObj);
    }

    @Override
    public void deletePlanTeacher(Long id) {
        // 校验存在
        this.validatePlanTeacherExists(id);
        // 删除
        planTeacherMapper.deleteById(id);
    }

    private void validatePlanTeacherExists(Long id) {
        if (planTeacherMapper.selectById(id) == null) {
            throw exception(PLAN_TEACHER_NOT_EXISTS);
        }
    }

    @Override
    public StudyPlanTeacherDO getPlanTeacher(Long id) {
        return planTeacherMapper.selectById(id);
    }

    @Override
    public List<StudyPlanTeacherDO> getPlanTeacherList(Collection<Long> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : planTeacherMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudyPlanTeacherDO> getPlanTeacherPage(StudyPlanTeacherPageReqVO pageReqVO) {
        return planTeacherMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudyPlanTeacherDO> getPlanTeacherList(StudyPlanTeacherExportReqVO exportReqVO) {
        return planTeacherMapper.selectList(exportReqVO);
    }

    /**
     * 根据课程id 获取教师id
     * @param subjectId
     * @return
     */
    @Override
    public String selectTeacherIdBySubjectId(String subjectId) {

        StudyPlanSubjectDO studyPlanSubjectDO = studyPlanSubjectMapper.selectOne(new LambdaQueryWrapper<StudyPlanSubjectDO>()
                .eq(StudyPlanSubjectDO::getStudySubjectId, subjectId)
                .last("limit 1"));

        if (ObjectUtil.isNull(studyPlanSubjectDO)) {
            return null;
        }
        StudyPlanTeacherDO studyPlanTeacherDO = planTeacherMapper.selectOne(new LambdaQueryWrapper<StudyPlanTeacherDO>()
                .eq(StudyPlanTeacherDO::getStudyPlanId, studyPlanSubjectDO.getStudyPlanId())
                .last("limit 1"));
        if (ObjectUtil.isNull(studyPlanTeacherDO)) {
            return null;
        }
        return studyPlanTeacherDO.getTeacherId();
    }

}
